package cn.aylog.test;

import cn.aylog.dao.UserDao;
import cn.aylog.pojo.User;
import com.alibaba.druid.pool.DruidDataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class SpringMybatisDemo {

   @Test
    public void run1() throws Exception{
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       DruidDataSource dataSource = context.getBean(DruidDataSource.class);
       Connection conn = dataSource.getConnection();
       PreparedStatement stmt = conn.prepareStatement("select * from `user`");
       ResultSet rs = stmt.executeQuery();
       System.out.println(rs);
   }

   @Test
   public void run2() throws Exception {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
       UserDao bean = context.getBean(UserDao.class);
       List<User> users = bean.selectAll();
       System.out.println(users);
   }

   @Autowired
   private UserDao dao;

   @Test
   public void run3() {
       List<User> users = dao.selectAll();
       System.out.println(users);
   }

}
